/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication2;



import java.io.File;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Matt
 */
public class Main {

    public static String getFilename(){
//        System.out.print("Enter filename: ");
//        Scanner scanner = new Scanner(System.in);
//        String filename = scanner.nextLine();
//        return filename;

        JFileChooser chooser = new JFileChooser();
        int status = chooser.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION){
            return chooser.getSelectedFile().getAbsolutePath();
        }
        return null;
    }

    public static int getDistance(){
        System.out.print("Enter cipher value: ");
        Scanner scanner2 = new Scanner(System.in);
        int distance = scanner2.nextInt();
        return distance;
    }

      private static void displayCipher(String filename, int distance){
        String code = "";

        try{
            Scanner input = new Scanner(new File(filename));
            
            while(input.hasNextLine()){
                String line = input.nextLine();
                System.out.println("Line contains: " + line);

                for (char ch : line.toCharArray()){
                    System.out.println("char out is: " + ch);

                    if(Character.isLetter(ch)){
                        int ordValue = (int)ch-(int)'a';
                        int newValue = (ordValue + distance) % 26;
                        int cipherValue = newValue + (int)'a';
                        code += (char)cipherValue;
                        
                    }else{
                        code += ch;
                    }
                }
                code+='\n';
            }

          }catch (Exception e){
            System.out.println("error: " + e);
          }
          System.out.println(code);
          JOptionPane.showMessageDialog(null,code,"Result",JOptionPane.INFORMATION_MESSAGE);
    }

  
  
    public static void main(String[] args) {
        displayCipher(getFilename(), getDistance());
        
        
       }
        
        
    }


